//
//  WDJLunPanEditController.m
//  wdj
//
//  Created by 蓝鳍互娱 on 2024/7/9.
//

#import "WDJLunPanEditController.h"
#import "WDJLunPanEditTableCell.h"
#import "WDJLunPanAddTableCell.h"
@interface WDJLunPanEditController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *lunPanTableView;
@property (nonatomic,strong) NSMutableArray *lunPans;
@end
@implementation WDJLunPanEditController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = true;
    self.lunPans = [NSMutableArray arrayWithArray:[WDJPublicData defaultManager].lunPans];
    [self wdjCreatView];
}
- (void)wdjCreatView{
    UIImageView * worldLunPanImgView = [[UIImageView alloc] init];
    worldLunPanImgView.image = [WDjWholeData wholeImageNamed:@"wdj_LunPanBg"];
    worldLunPanImgView.userInteractionEnabled = true;
    [self.view addSubview:worldLunPanImgView];
    [worldLunPanImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@(0));
    }];
    UIButton * lunPanNavBackBtn = [[UIButton alloc] init];
    [lunPanNavBackBtn setBackgroundImage:[WDjWholeData wholeImageNamed:@"wdj_NavBack"] forState:UIControlStateNormal];
    [lunPanNavBackBtn addTarget:self action:@selector(lunPanNavBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:lunPanNavBackBtn];
    [lunPanNavBackBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([WDjWholeData wholeScreewRatio:24]));
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:64]));
        make.width.equalTo(@([WDjWholeData wholeScreewRatio:64]));
        make.top.equalTo(@([WDjWholeData wholeScreewRatio:12]+[WDjWholeData wholeTopSafeH]));
    }];
    UILabel * lunPanNavTitleLab = [[UILabel alloc] init];
    lunPanNavTitleLab.font = [UIFont systemFontOfSize:[WDjWholeData wholeScreewRatio:34] weight:UIFontWeightRegular];
    lunPanNavTitleLab.textColor = [WDjWholeData wholeColorHexString:@"#FFFFFF"];
    lunPanNavTitleLab.text = @"编辑轮盘";
    [self.view addSubview:lunPanNavTitleLab];
    [lunPanNavTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lunPanNavBackBtn);
        make.centerX.equalTo(self.view);
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:50]));
    }];
    UILabel * lunPanHeadLab = [[UILabel alloc] init];
    lunPanHeadLab.font = [UIFont systemFontOfSize:[WDjWholeData wholeScreewRatio:28] weight:UIFontWeightRegular];
    lunPanHeadLab.textColor = [WDjWholeData wholeColorHexString:@"#9FA4B8"];
    lunPanHeadLab.text = @"转盘选项";
    [self.view addSubview:lunPanHeadLab];
    [lunPanHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([WDjWholeData wholeScreewRatio:29]));
        make.top.equalTo(@([WDjWholeData wholeScreewRatio:131]+[WDjWholeData wholeTopSafeH]));
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:39]));
    }];
    self.lunPanTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.lunPanTableView.delegate = self;
    self.lunPanTableView.dataSource = self;
    self.lunPanTableView.backgroundColor = [UIColor clearColor];
    self.lunPanTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.lunPanTableView];
    [self.lunPanTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lunPanHeadLab.mas_bottom).offset([WDjWholeData wholeScreewRatio:19]);
        make.left.equalTo(@([WDjWholeData wholeScreewRatio:0]));
        make.right.equalTo(@([WDjWholeData wholeScreewRatio:0]));
        make.bottom.equalTo(@(-[WDjWholeData wholeScreewRatio:350]));
    }];
    UIButton * lunPanSaveBtn = [[UIButton alloc] init];
    [lunPanSaveBtn setBackgroundImage:[WDjWholeData wholeImageNamed:@"wdj_LunPanSave"] forState:UIControlStateNormal];
    [lunPanSaveBtn addTarget:self action:@selector(lunPanSave) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:lunPanSaveBtn];
    [lunPanSaveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([WDjWholeData wholeScreewRatio:24]));
        make.right.equalTo(@(-[WDjWholeData wholeScreewRatio:24]));
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:88]));
        make.bottom.equalTo(@(-[WDjWholeData wholeScreewRatio:58]));
    }];
}
- (void)lunPanSave{
    if(self.lunPans.count > 0){
        NSString * lastLunPan = self.lunPans.lastObject;
        if([lastLunPan isEqualToString:@""]){
            [self.lunPans removeObjectAtIndex:(self.lunPans.count - 1)];
        }
    }
    [WDJPublicData defaultManager].lunPans = self.lunPans;
    [[NSUserDefaults standardUserDefaults] setObject:[WDJPublicData defaultManager].lunPans forKey:@"lunPanTitle"];
    if (self.LunPanSaveBlock) {
        self.LunPanSaveBlock();
    }
    [SVProgressHUD showInfoWithStatus:@"保存成功"];
}
- (void)lunPanNavBack{
    [self.navigationController popViewControllerAnimated:true];
}
#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.lunPans.count+1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [WDjWholeData wholeScreewRatio:116];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WeakSelf
    if(indexPath.row != (self.lunPans.count)){
        WDJLunPanEditTableCell *lunPanEditTableCell = [WDJLunPanEditTableCell lunPanEditTableCell:tableView];
        lunPanEditTableCell.lunPanEditName = self.lunPans[indexPath.row];
        lunPanEditTableCell.LunPanDelectBlock = ^{
            [weakSelf.lunPans removeObjectAtIndex:indexPath.row];
            [weakSelf.lunPanTableView reloadData];
        };
        lunPanEditTableCell.LunPanEditBlock = ^(NSString * _Nonnull lunPanEdit) {
            [weakSelf.lunPans replaceObjectAtIndex:(weakSelf.lunPans.count-1) withObject:lunPanEdit];
            [weakSelf.lunPanTableView reloadData];
        };
        return lunPanEditTableCell;
    }else{
        WDJLunPanAddTableCell *lunPanAddTableCell = [WDJLunPanAddTableCell lunPanAddTableCell:tableView];
        lunPanAddTableCell.LunPanAddBlock = ^{
            if(weakSelf.lunPans.count > 0){
                NSString * lastLunPan = self.lunPans.lastObject;
                if(lastLunPan.length > 0){
                    [weakSelf.lunPans addObject:@""];
                }
            }else{
                [weakSelf.lunPans addObject:@""];
            }
            [weakSelf.lunPanTableView reloadData];
        };
        return lunPanAddTableCell;
    }
}
//-(UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath{
////    LQGuanZhuModel *guanZhuMdl = self.roomGaunZhuTables[indexPath.row-1];
//    UIContextualAction *deleteActionCancleCollect = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)){
//        [LbbDelectRecordAlertView showDelectRecordAlertComplaint:^{
//            [self delectRecord:indexPath];
//        } withSuperView:self.view];
//        [tableView setEditing:NO animated:YES];
//    }];
//    deleteActionCancleCollect.backgroundColor = LbbHex(@"#F9789E");
//    NSArray<UIContextualAction*> *contextualAction = @[deleteActionCancleCollect];
//    UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:contextualAction];
//    actions.performsFirstActionWithFullSwipe = NO;       // 禁止侧滑无线拉伸
//    return actions;
//}
@end
